<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\jzshop\model;

use app\jzshop\common\Tools;
use think\Model as ThinkModel;

/**
 * 模型
 * @package app\jzshop\model
 */
class GoodsSkuStandby extends ThinkModel
{
    // 设置当前模型对应的完整数据表名称
    protected $name = 'jzshop_goods_standby';

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;

    const STATUS = ['未推送', '已推送', '重新推送'];
    const YPAPP_STATUS = ['未上架', '已上架', '已下架'];

    static function saveGoods($goodsSku)
    {

        $where = ['supplierId' => $goodsSku['supplierId'], 'itemSpuId' => $goodsSku['itemSpuId'], 'itemId' => $goodsSku['itemId']];
        $res =  self::where($where)->find();
        $goodsSku = $goodsSku->toArray();
        $goodsSku = self::getImageUrlToAttachment($goodsSku);
        unset($goodsSku['id']);
        $goodsSku['itemCodeAlias'] = 'csmb_' . strTools($goodsSku['itemCode']); //'csmb_' . $goodsSku['itemCode'];
        $goodsSku['itemNameAlias'] = $goodsSku['itemName'];
        $goodsSku['colourAlias'] = $goodsSku['colour'];
        $goodsSku['status'] = 1;
        if (!$res) {
            self::create($goodsSku);
        } else {
            $status = self::where($where)->value('status');
            if ($status == 0) {
                self::where($where)->update($goodsSku);
            }
        }
    }

    static function getImageUrlToAttachment($goodsSku)
    {

        try {
            $saveImg = Tools::downImage($goodsSku['pic']);
            $goodsSku['main_img'] = $saveImg['id'];
            $goodsSku['main_img_alias'] = $saveImg['id'];
        } catch (\Throwable $th) {
        }

        try {
            //code...

            $where = [
                'supplierId' => $goodsSku['supplierId'],
                'itemSpuId' => $goodsSku['itemSpuId'],
            ];
            $goods = Goods::where($where)->find();

            $itemMainImage = json_decode($goods['itemMainImage'], true);
            $header_imgs = [];
            foreach ($itemMainImage as $img) {
                $saveImg =    Tools::downImage($img);
                $header_imgs[] = $saveImg['id'];
            }
            $goodsSku['header_imgs']  = join(',', $header_imgs);
        } catch (\Throwable $th) {
            //throw $th;
        }
        try {
            //code...

            $itemDetailImages = json_decode($goods['itemDetailImages'], true);
            $body_imgs = [];
            foreach ($itemDetailImages as $image) {

                $saveImg = Tools::downImage($image['imageUrl']);
                $body_imgs[] = $saveImg['id'];
            }
            $goodsSku['body_imgs']  = join(',', $body_imgs);
        } catch (\Throwable $th) {
            //throw $th;
        }
        return $goodsSku;
    }

    static public function status($id, $status)
    {
        self::where('id', $id)->update(['status' => $status]);
    }
}
